/*
 * jcr.cpp
 *
 *  Created on: Feb 15, 2012
 *      Author: fumin
 */

#include "jcr.h"

void init_jcr(char *path)
{
    jcr.path = sdsnew(path);

    struct stat s;
    if (stat(path, &s) != 0)
    {
        fprintf(stderr, "backup path does not exist!");
        exit(1);
    }
    if (S_ISDIR(s.st_mode) && jcr.path[sdslen(jcr.path) - 1] != '/')
        jcr.path = sdscat(jcr.path, "/");

    jcr.bv = NULL;

    jcr.id = TEMPORARY_ID;

    jcr.status = JCR_STATUS_INIT;

    jcr.file_num = 0;
    jcr.data_size = 0;
    jcr.unique_data_size = 0;
    jcr.delta_chunk_num = 0;
    jcr.delta_data_size = 0;
    jcr.chunk_num = 0;
    jcr.unique_chunk_num = 0;
    jcr.zero_chunk_num = 0;
    jcr.zero_chunk_size = 0;
    jcr.rewritten_chunk_num = 0;
    jcr.rewritten_chunk_size = 0;

    jcr.rewritten_base_chunk_num = 0;
    jcr.rewritten_base_chunk_size = 0;

    jcr.base_chunk_num_in_ver1 = 0;
    jcr.base_container_in_ver1 = 0;
    jcr.percent_base_container_in_ver1;

    jcr.container_num_before_backup = 0;
    jcr.container_num_stored = 0;
    jcr.container_num_after_backup = 0;

    jcr.container_num_now_written = 0;

    jcr.last_container_number_of_backup = 0;

    jcr.container_num_now_stored = 0;

    jcr.total_container_num_referred = 0;
    jcr.sparse_container_num = 0;
    jcr.inherited_sparse_num = 0;

    jcr.base_chunk_referred = 0;
    jcr.base_container_referred = 0;

    jcr.container_num_of_first_backup = 0;
    jcr.base_container_in_sparse = 0;
    jcr.base_chunk_in_sparse_container = 0;

    jcr.unique_containers_in_restore = 0;

    jcr.total_time = 0;
    /*
	 * the time consuming of seven backup phase
	 */
    jcr.read_time = 0;
    jcr.chunk_time = 0;
    jcr.hash_time = 0;
    jcr.dedup_time = 0;
    jcr.rewrite_time = 0;
    jcr.filter_time = 0;
    jcr.write_time = 0;

    /*
	 * the time consuming of three restore phase
	 */
    jcr.read_recipe_time = 0;
    jcr.read_chunk_time = 0;
    jcr.write_chunk_time = 0;
    jcr.super_feature_time = 0;

    jcr.matching_time = 0;
    jcr.delta_time = 0;

    jcr.read_container_num = 0;

    jcr.base_cache_hit = 0;
    jcr.base_cache_miss = 0;

    jcr.base_container_read = 0;

    jcr.sparse_baseExist_rewritten_num = 0;

    jcr.containers_to_restore = 0;
    jcr.predict_restore_speed_factor = 0;
    jcr.dedup_delta_chunks = 0;
    jcr.total_sf_num = 0;
    jcr.freed_delta_number = 0;
    jcr.number_of_chunks_has_delta = 0;
    jcr.number_of_ofc_chunks = 0;
    jcr.duplicate_has_delta = 0;
    jcr.noduplicate_has_outoforder = 0;
    jcr.delta_compression_times = 0;
    jcr.read_normal_chunks = 0;
    jcr.read_base_chunks = 0;
    jcr.hit_normal_chunks = 0;
    jcr.hit_base_chunks = 0;
    jcr.hit_sparse_base_cid = 0;
    jcr.miss_sparse_base_cid = 0;

    jcr.slack_content = 0;
    jcr.delta_compressed_size = 0;
    jcr.size_before_compressed = 0;

    jcr.unproper_delta_comp_num = 0;
    jcr.total_size_for_similar_detection = 0;
    jcr.total_compressed_size = 0;
    jcr.total_size_for_deltacompression = 0;
    jcr.chunk_num_for_simi_dete = 0;
    jcr.sketch_replace_time = 0;
    jcr.sketch_index_item_num = 0;

    jcr.stored_data_size = 0;

    jcr.sf_matched_number = 0;
    jcr.multi_matched_chunk_number = 0;
    jcr.multi_matched_diff_number = 0;
}

void init_backup_jcr(char *path)
{

    init_jcr(path);
    jcr.file_num = destor.file_num;
    jcr.bv = create_backup_version(jcr.path);

    jcr.id = jcr.bv->bv_num;
    jcr.container_num_before_backup = get_container_count();
    if (jcr.container_num_before_backup != 0)
        jcr.container_num_now_stored = jcr.container_num_before_backup - 1;
}

void init_restore_jcr(int revision, char *path)
{

    init_jcr(path);

    jcr.bv = open_backup_version(revision);

    if (jcr.bv->deleted == 1)
    {
        WARNING("The backup has been deleted!");
        exit(1);
    }

    jcr.id = revision;
}
